using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomPropertyDrawer(typeof(C_Anim))]
public class C_AnimDrawer : PropertyDrawer
{
    public SerializedProperty C_AnimName;
    public SerializedProperty C_IsDefaultAnim;
    public SerializedProperty C_IsLoop;
    public SerializedProperty C_AnimClip;
    public SerializedProperty C_AnimEventList;
    public SerializedProperty foldout;
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        using (new EditorGUI.PropertyScope(position, label, property))
        {
            C_AnimName = property.FindPropertyRelative("C_AnimName");
            C_IsDefaultAnim = property.FindPropertyRelative("C_IsDefaultAnim");
            C_IsLoop = property.FindPropertyRelative("C_IsLoop");
            C_AnimClip = property.FindPropertyRelative("C_AnimClip");
            C_AnimEventList = property.FindPropertyRelative("C_AnimEventList");
            foldout = property.FindPropertyRelative("foldout");
            //label宽度
            EditorGUIUtility.labelWidth = 120;
            position.height = EditorGUIUtility.singleLineHeight;

            var foldoutRect = new Rect(position)
            {
                width = position.width,
                height = position.height,
            };
            var C_AnimNameRect = new Rect(foldoutRect)
            {
                y = foldoutRect.y + position.height + 5,
            };
            var C_IsDefaultAnimRect = new Rect(C_AnimNameRect)
            {
                y = C_AnimNameRect.y + position.height + 5,
            };
            var C_IsLoopRect = new Rect(C_IsDefaultAnimRect)
            {
                y = C_IsDefaultAnimRect.y + position.height + 5,
            };
            var C_AnimClipRect = new Rect(C_IsLoopRect)
            {
                y = C_IsLoopRect.y + position.height + 5,
            };
            var C_AnimEventListRect = new Rect(C_AnimClipRect)
            {
                y = C_AnimClipRect.y + position.height + 5,
            };
            foldout.boolValue = EditorGUI.Foldout(foldoutRect, foldout.boolValue, C_AnimName.stringValue, true);
            if (foldout.boolValue)
            {
                if(C_AnimName.stringValue == "")
                {
                    C_AnimName.stringValue = "DefaultAnim";
                }
                EditorGUI.PropertyField(C_AnimNameRect, C_AnimName, new GUIContent("动画名称:"), true);
                EditorGUI.PropertyField(C_IsDefaultAnimRect, C_IsDefaultAnim, new GUIContent("是否为默认动画:"), true);
                EditorGUI.PropertyField(C_IsLoopRect, C_IsLoop, new GUIContent("是否循环:"), true);
                EditorGUI.PropertyField(C_AnimClipRect, C_AnimClip, new GUIContent("动画Clip（自动生成）:"), true);
                EditorGUI.indentLevel++;
                EditorGUI.PropertyField(C_AnimEventListRect, C_AnimEventList, new GUIContent("动画Event:"), true);
                EditorGUI.indentLevel--;
            }
        }
    }
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        foldout = property.FindPropertyRelative("foldout");
        if (!foldout.boolValue)
        {
            return base.GetPropertyHeight(property, label);
        }
        else
        {
            return base.GetPropertyHeight(property, label) * 5 + 5 * 5 + EditorGUI.GetPropertyHeight(C_AnimEventList = property.FindPropertyRelative("C_AnimEventList"));
        }
    }
}
